/**
 * 报表管理
 */
var report={
		
	ctx : "",
	
	/**查询员工报表*/
	queryStaff : function(currentPage){
		var startDate = $("#startDate").val();
		var endDate = $("#endDate").val();
		var staffId = $("#staffId1").val();
		$.ajax({
			url  : report.ctx + "/staffReportPage",
			type : "post",
			async:false,
			dataType:"json", 
			data: {	
				"currentPage":currentPage,
				"startDate":startDate,
				"endDate":endDate,
				"staffId":staffId
			},
			success:function(data){
				var count = data.count;
				var list = data.projectList
				var str = "";
				var sumCount = 0;
				
				for(var i=0;i<list.length;i++){
					var config = list[i];
					str = str + "<tr>"
							+ "<td><a href=\" "+report.ctx+"/project/projectQueryFormPage?projectId="+list[i].projectId+" \" >" + list[i].projectName+"</a></td>"
							+ "<td><fmt:formatDate value='" + list[i].signDate+"' pattern='yyyy年MM月dd日'/></td>"
							+ "<td>" + list[i].customerCompanyName + "</td>"
							+ "<td>" + list[i].offer + "</td>"
							+ "</tr>";
					
					sumCount = sumCount + parseFloat(list[i].offer);
				}
				
				str = str + 
					"<tr>" +
						"<td></td>" +
						"<td></td>" +
						"<td>加总:</td>" +
						"<td>" + sumCount + "</td>" +
					"</tr>";
				
				$('#tableList').html(str);
				var container = $('#page');
				report.setPage(container, count, currentPage,"report.queryStaff");
	       	},
	       	error: function(){ 
	       		$('#error').val("出现异常！");
				$('#errorMsg').html("出现异常,或没有找到相关数据");
				var $modal1 = $('#my-alert');
				$modal1.modal();
	        }
		}); 
	},
	/**查询客户报表*/
	queryCustomer : function(currentPage){
		var startDate = $("#startDate").val();
		var endDate = $("#endDate").val();
		var customerCompanyId = $("#customerCompanyId").val();
		
		$.ajax({
			url  : report.ctx + "/customerReportPage",
			type : "post",
			async:false,
			dataType:"json", 
			data: {	
				"currentPage":currentPage,
				"startDate":startDate,
				"endDate":endDate,
				"customerCompanyId":customerCompanyId
			},
			success:function(data){
				var count = data.count;
				var list = data.projectList
				var str = "";
				var sumCount = 0;
				
				for(var i=0;i<list.length;i++){
					var config = list[i];
					str = str 
						+ "<tr>" 
						+ "<td><a href=\"" + report.ctx + "/project/projectQueryFormPage?projectId="+list[i].projectId + " \" >" + list[i].projectName+"</a></td>"
						+ "<td><fmt:formatDate value='" + list[i].signDate+"' pattern='yyyy年MM月dd日'/></td>"
						+ "<td>" + list[i].staffName + "</td>"
						+ "<td>" + list[i].offer + "</td>"
						+ "</tr>";
					
					sumCount = sumCount + parseFloat(list[i].offer);
				}
				
				str = str + 
					"<tr>" +
						"<td></td>" +
						"<td></td>" +
						"<td>加总:</td>" +
						"<td>" + sumCount + "</td>" +
					"</tr>";
				
				$('#tableList').html(str);
				var container = $('#page');
				report.setPage(container, count, currentPage, "report.queryCustomer");
	       	},
	       	error: function(){ 
	       		$('#error').val("出现异常！");
				$('#errorMsg').html("出现异常,或没有找到相关数据");
				var $modal1 = $('#my-alert');
				$modal1.modal();
	        }
		}); 
	},
	/**查询公司报表*/
	queryCompany : function(currentPage){
		var startDate = $("#startDate").val();
		var endDate = $("#endDate").val();
		var companyId = $("#companyId").val();
		
		$.ajax({
			url  : report.ctx + "/companyReportPage",
			type : "post",
			async:false,
			dataType:"json", 
			data: {	
				"currentPage":currentPage,
				"startDate":startDate,
				"endDate":endDate,
				"companyId":companyId
			},
			success:function(data){
				var count = data.count;
				var list = data.projectList
				var str = "";
				var sumCount = 0;
				
				for(var i=0;i<list.length;i++){
					var config = list[i];
					str = str 
						+ "<tr>" 
						+ "<td><a href=\"" + report.ctx + "/project/projectQueryFormPage?projectId="+list[i].projectId + " \" >" + list[i].projectName+"</a></td>"
						+ "<td><fmt:formatDate value='" + list[i].signDate + "' pattern='yyyy年MM月dd日'/></td>"
						+ "<td>" + list[i].staffName + "</td>"
						+ "<td>" + list[i].customerCompanyName + "</td>"
						+ "<td>" + list[i].offer + "</td>"
						+ "</tr>";
					
					sumCount = sumCount + parseFloat(list[i].offer);
				}
				
				str = str + 
					"<tr>" +
						"<td></td>" +
						"<td></td>" +
						"<td></td>" +
						"<td>加总:</td>" +
						"<td>" + sumCount + "</td>" +
					"</tr>";
				
				$('#tableList').html(str);
				var container = $('#page');
				report.setPage(container, count, currentPage, "report.queryCompany");
	       	},
	       	error: function(){ 
	       		$('#error').val("出现异常！");
				$('#errorMsg').html("出现异常,或没有找到相关数据");
				var $modal1 = $('#my-alert');
				$modal1.modal();
	        }
		}); 
	},
	/**查询项目机会报表*/
	queryProject : function(currentPage){
		var startDate = $("#startDate").val();
		var endDate = $("#endDate").val();
		var staffId = $("#staffId1").val();
		
		$.ajax({
			url  : report.ctx + "/projectReportPage",
			type : "post",
			async:false,
			dataType:"json", 
			data: {	
				"currentPage":currentPage,
				"startDate":startDate,
				"endDate":endDate,
				"staffId":staffId
			},
			success:function(data){
				var count = data.count;
				var list = data.projectList
				var str = "";
				var sumCount = 0;
				
				for(var i=0;i<list.length;i++){
					var config = list[i];
					str = str 
						+ "<tr>" 
						+ "<td><a href=\"" + report.ctx + "/project/projectQueryFormPage?projectId="+list[i].projectId + " \" >" + list[i].projectName+"</a></td>"
						+ "<td><fmt:formatDate value='" + list[i].expectedDealDate + "' pattern='yyyy年MM月dd日'/></td>"
						+ "<td>" + list[i].staffName + "</td>"
						+ "<td>" + list[i].customerCompanyName + "</td>"
						+ "<td>" + list[i].offer + "</td>"
						+ "</tr>";
					
					sumCount = sumCount + parseFloat(list[i].offer);
				}
				
				str = str + 
					"<tr>" +
						"<td></td>" +
						"<td></td>" +
						"<td></td>" +
						"<td>加总:</td>" +
						"<td>" + sumCount + "</td>" +
					"</tr>";
				
				$('#tableList').html(str);
				var container = $('#page');
				report.setPage(container, count, currentPage, "report.queryProject");
	       	},
	       	error: function(){ 
	       		$('#error').val("出现异常！");
				$('#errorMsg').html("出现异常,或没有找到相关数据");
				var $modal1 = $('#my-alert');
				$modal1.modal();
	        }
		}); 
	},
	/**查询项目未收款报表*/
	queryNotPayment : function(currentPage){
		var startDate = $("#startDate").val();
		var endDate = $("#endDate").val();
		var staffId = $("#staffId1").val();
		
		$.ajax({
			url  : report.ctx + "/notPaymentReportPage",
			type : "post",
			async:false,
			dataType:"json", 
			data: {	
				"currentPage":currentPage,
				"startDate":startDate,
				"endDate":endDate,
				"staffId":staffId
			},
			success:function(data){
				var count = data.count;
				var list = data.projectList
				var str = "";
				var sumCount = 0;
				
				for(var i=0;i<list.length;i++){
					var config = list[i];
					str = str 
						+ "<tr>" 
						+ "<td>" + list[i].customerCompanyName + "</td>"
						+ "<td><a href=\"" + report.ctx + "/project/projectImplementFormPage?projectId="+list[i].projectId + " \" >" + list[i].projectName+"</a></td>"
						+ "<td>" + list[i].staffName + "</td>"
						+ "<td>" + list[i].signDateStr + "</td>"
						+ "<td>" + list[i].offer + "</td>"
						+ "<td>" + list[i].billingRate + "%</td>"
						+ "<td>" + list[i].billingMoney + "</td>"
						+ "<td>" + list[i].paymentRate + "%</td>"
						+ "<td>" + list[i].paymentMoney + "</td>"
						+ "</tr>";
					
					sumCount = sumCount + parseFloat(list[i].paymentMoney);
				}
				
				str = str + 
					"<tr>" +
						"<td></td>" +
						"<td></td>" +
						"<td></td>" +
						"<td></td>" +
						"<td></td>" +
						"<td></td>" +
						"<td></td>" +
						"<td>加总:</td>" +
						"<td>" + sumCount + "</td>" +
					"</tr>";
				
				$('#tableList').html(str);
				var container = $('#page');
				report.setPage(container, count, currentPage, "report.queryNotPayment");
	       	},
	       	error: function(){ 
	       		$('#error').val("出现异常！");
				$('#errorMsg').html("出现异常,或没有找到相关数据");
				var $modal1 = $('#my-alert');
				$modal1.modal();
	        }
		}); 
	},
	/**分页工具*/
	//container 容器,count 总页数, pageindex 当前页数,action 调用的查询方法名称
	setPage : function(container, count, pageindex,action) {
		var container = container;
		var count = count;
		var pageindex = pageindex;

		var a = [];
		  //总页数少于10 全部显示,大于10 显示前3 后3 中间3 其余....
		  if (pageindex == 1) {
			a[a.length] = "<li class=\"am-disabled\"><a href=\"javascript:void(0);\" class=\"prev unclick\" onclick=\""+action+"('1')\"> « </a></li>";
		  } else {
			a[a.length] = "<li><a href=\"javascript:void(0);\" class=\"prev\" onclick=\" "+action+"('"+(pageindex-1)+"')\"> « </a></li>";
		  }
		  function setPageList() {
			if (pageindex == i) {
			  a[a.length] = "<li class=\"am-active\"><a href=\"javascript:void(0);\" onclick=\""+action+"('"+i+"')\">" + i + "</a></li>";
			} else {
			  a[a.length] = "<li><a href=\"javascript:void(0);\" onclick=\""+action+"('"+i+"')\">" + i + "</a></li>";
			}
		  }
		  //总页数小于5
		  if (count <= 4) {
			for (var i = 1; i <= count; i++) {
			  setPageList();
			}
		  }
		  //总页数大于4页
		  else {
			if (pageindex <= 3) {
			  for (var i = 1; i <= 3; i++) {
				setPageList();
			  }
			  a[a.length] = "<li>......&nbsp;&nbsp;</li><li><a href=\"javascript:void(0);\" onclick=\""+action+"('"+count+"')\">" + count + "</a></li>";
			} else if (pageindex >= count - 2) {
			  a[a.length] = "<li><a href=\"javascript:void(0);\" onclick=\""+action+"('1')\">1</a></li><li>......&nbsp;&nbsp;</li>";
			  for (var i = count - 2; i <= count; i++) {
				setPageList();
			  }
			} else { //当前页在中间部分
			  a[a.length] = "<li><a href=\"javascript:void(0);\" onclick=\""+action+"('1')\">1</a></li><li>......&nbsp;&nbsp;</li>";
			  for (var i = pageindex - 1; i <= pageindex + 1; i++) {
				setPageList();
			  }
			  a[a.length] = "<li>......&nbsp;&nbsp;</li><li><a href=\"javascript:void(0);\" onclick=\""+action+"('"+count+"')\">" + count + "</a></li>";
			}
		  }
		  if (pageindex == count) {
			a[a.length] = "<li class=\"am-disabled\"><a href=\"javascript:void(0);\" class=\"next unclick\" onclick=\""+action+"('"+count+"')\"> » </a></li>";
		  } else {
			  var addindex = parseInt(pageindex) + 1;
			a[a.length] = "<li><a href=\"javascript:void(0);\" class=\"next\" onclick=\""+action+"('"+addindex+"')\"> » </a></li>";
		  }
		  
		  container.html(a) ;
	}
		
		
}